package com.csiec.clock.view.model;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.text.InputType;
import android.widget.Toast;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.csiec.clock.R;
import com.csiec.clock.activity.AboutActivity;
import com.csiec.clock.activity.LoginActivity;
import com.csiec.clock.activity.UserInfoActivity;
import com.csiec.clock.activity.daka.IdeaActivity;
import com.csiec.clock.bean.User;
import com.csiec.clock.bean.UserInfo;
import com.csiec.clock.bean.clock.BackData;
import com.csiec.clock.callback.JoinCallBack;
import com.csiec.clock.databinding.MineFragmentBinding;
import com.csiec.clock.model.JoinModel;
import com.csiec.clock.utils.Constant;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

public class MineViewModel extends ViewModel {
    private Activity activity;
    private MineFragmentBinding binding;
    private MutableLiveData<String> id;
    private MutableLiveData<String> studentNumber;
    private MutableLiveData<String> status;
    private User user;
    private Intent myIntent;

    private final Handler handler = new Handler();
    private JoinModel model;

    public void init(Activity activity, MineFragmentBinding binding) {
        model = new JoinModel();
        this.activity = activity;
        this.binding = binding;
        user = Constant.checkStudentOrTeacher(activity);
        if (id == null) id = new MutableLiveData<>();
        if (status == null) status = new MutableLiveData<>();
        if (studentNumber == null) studentNumber = new MutableLiveData<>();
        getSomeMessage();
    }

    private void getSomeMessage() {
        SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
    }

    //退出登录
    public void exitLogin() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("提示");
        builder.setMessage("是否退出登录？");
        builder.setPositiveButton("确定", (dialog, which) -> {
            Intent intent = new Intent(activity, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent);
            activity.finish();
            clearSave(activity.getApplicationContext());
            dialog.dismiss();

        });
        builder.setNegativeButton("取消", (dialog, which) -> dialog.dismiss());
        AlertDialog dialog = builder.create();  //创建AlertDialog对象
        dialog.show();                           //显示对话框
    }

    //跳转到管理个人信息页面
    public void userInfo() {
        Intent intent = new Intent(activity, UserInfoActivity.class);
        activity.startActivity(intent);
    }

    //跳转到关于页面
    public void aboutMessage() {
        Intent intent = new Intent(activity, AboutActivity.class);
        activity.startActivity(intent);
    }

    //跳转到意见反馈页面
    public void idea() {
        Intent intent = new Intent(activity, IdeaActivity.class);
        activity.startActivity(intent);
    }

    //添加打卡
    public void addDaka() {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(activity);
        builder.setTitle("添加打卡")
                .setPlaceholder("请输入分享码")
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            addClock(text.toString());
                            dialog.dismiss();
                        } else {
                            showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "请填入分享码");
                        }
                    }
                })
                .show();
    }

    //添加打卡的网络请求
    private void addClock(String text) {
        model.addClockIn(text, new JoinCallBack() {
            @Override
            public void onSuccess(BackData backData) {
                if (backData.getStatus() == 200) {
                    handler.post(() -> {
                        showTip(QMUITipDialog.Builder.ICON_TYPE_SUCCESS, "加入成功");
                    });
                } else {
                    handler.post(() -> {
                        showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "加入失败");
                    });
                }
            }

            @Override
            public void onFailed() {
                handler.post(() -> {
                    showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "网络异常");
                });
            }
        });
    }

    public void changeMsg() {
        studentNumber.setValue("万众一心 共克时艰");
    }

    //清除缓存
    private void clearSave(Context context) {
        SharedPreferences.Editor editor = context.getSharedPreferences("data", Context.MODE_PRIVATE).edit();
        editor.putBoolean("isLogin", false);
        editor.apply();
    }

    public MutableLiveData<String> getId() {
        SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);

        id.setValue(preferences.getString("username", ""));
        return id;
    }

    //提示悬浮窗
    public void showTip(int type, String msg) {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(type)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        binding.textView33.postDelayed(tipDialog::dismiss, 2000);
    }

    public MutableLiveData<String> getStatus() {
        if (UserInfo.getUserInfo().getStudentNumber() != null) {
            status.setValue("(身份已认证)");
        } else {
            status.setValue("(身份未认证)");
        }
        return status;
    }

    public MutableLiveData<String> getStudentNumber() {
        if (UserInfo.getUserInfo().getStudentNumber() != null) {
            studentNumber.setValue(UserInfo.getUserInfo().getStudentNumber());
        }
        return studentNumber;
    }
}
